//
//  MyStoreViewController.m
//  LastOne
//
//  Created by 小柠檬 on 15/11/9.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "MyStoreViewController.h"
#import "FitHeader.h"
#import "CoreDataManager.h"
#import "HQHListDetailTableViewCell.h"
#import "NightHeader.h"
#import "HQHListDetailWebViewController.h"
#import "MyStoreModel.h"
#import "MySearchViewController.h"
@interface MyStoreViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *dataSourceArray;
@property(nonatomic, strong)CoreDataManager *manager;
@property(nonatomic, strong)UILabel *flagLabel;

@end

@implementation MyStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的收藏";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-sousuo"] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    
    _manager = [CoreDataManager shareManager];
    
      self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 554 * FitHeight)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UITableView alloc]init];
    
    [self.view addSubview:_tableView];
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160 * FitHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    HQHListDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HQHListDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.listDetailModel = _dataSourceArray[indexPath.row];
    [cell NightWithType:UIViewColorTypeNormal];
    return cell;
}

-(void)viewWillAppear:(BOOL)animated
{
   NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"HQHReadSave"];
    _dataSourceArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    [_tableView reloadData];
    
    
    //暂无数据
    self.flagLabel = [[UILabel alloc] init];
    _flagLabel.textColor = [UIColor darkGrayColor];
    _flagLabel.text = @"暂无收藏";
    _flagLabel.textAlignment = 1;
    _flagLabel.font = [UIFont systemFontOfSize:25];
    [self.view addSubview:_flagLabel];
    
    if (_dataSourceArray.count == 0) {
        _flagLabel.frame = CGRectMake(0, 0 * FitHeight, self.view.frame.size.width, 554 * FitHeight);
        _flagLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        
    }
    else{
        _flagLabel.frame = CGRectMake(100 * FitWidth, 667 * FitHeight, self.view.frame.size.width, 100 * FitHeight);
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HQHListDetailWebViewController *web = [[HQHListDetailWebViewController alloc]init];
    web.model = _dataSourceArray[indexPath.row];
    [self.navigationController pushViewController:web animated:YES];
}

-(void)searchAction
{
    MySearchViewController *searchVC = [[MySearchViewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
